using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace Umdf.SideShow.Scf.ObjectModel
{
    public class ScfDialog : ScfBodyElement
    {
        private uint _imageId;
        private List<ScfText> _text;
        private List<ScfButton> _buttons;

        public uint ImageId
        {
            get { return _imageId; }
            set { _imageId = value; }
        }

        public List<ScfText> Text
        {
            get
            {
                if (_text == null)
                {
                    _text = new List<ScfText>();
                }
                return _text;
            }
        }

        public List<ScfButton> Buttons
        {
            get
            {
                if (_buttons == null)
                {
                    _buttons = new List<ScfButton>();
                }
                return _buttons;
            }
        }

        protected internal override void DeserializeFromXml(System.Xml.XmlNode node)
        {
            base.DeserializeFromXml(node);
            _imageId = GetAttribute<uint>(node, "imgid");
            foreach (XmlNode childNode in node.ChildNodes)
            {
                switch (childNode.LocalName)
                {
                    case "txt":
                        ScfText txt = new ScfText();
                        txt.DeserializeFromXml(childNode);
                        Text.Add(txt);
                        break;
                    case "btn":
                        ScfButton btn = new ScfButton();
                        btn.DeserializeFromXml(childNode);
                        Buttons.Add(btn);
                        break;
                }
            }
        }

        protected internal override void SerializeObject(System.IO.BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_imageId);
            writer.Write(Text.Count);
            foreach (ScfText t in Text)
            {
                writer.Write(t.GetType().FullName);
                t.SerializeObject(writer);
            }
            writer.Write(Buttons.Count);
            foreach (ScfButton b in Buttons)
            {
                writer.Write(b.GetType().FullName);
                b.SerializeObject(writer);
            }
        }

        protected internal override void DeserializeObject(System.IO.BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _imageId = reader.ReadUInt32();
            int count = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                string typeName = reader.ReadString();
                Type t = Type.GetType(typeName);
                ScfText txt = (ScfText)Activator.CreateInstance(t);
                Text.Add(txt);
            }
            count = reader.ReadInt32();
            for (int i = 0; i < count; i++)
            {
                string typeName = reader.ReadString();
                Type t = Type.GetType(typeName);
                ScfButton b = (ScfButton)Activator.CreateInstance(t);
                Buttons.Add(b);
            }
        }
    }
}
